<template>
  <div class="about">
    <div class="title">
      <div>
        <span>TODOLIST</span>
        <input type="text"
               v-model="txt"
               @keydown.enter="send()">
      </div>
    </div>
    <div class="doing">
      <h1>正在进行</h1>
      <ul class="list">
        <li v-for="doingItem in dongingList"
            :key="doingItem.id">
          <input type="checkbox"
                 @click="changedoing(doingItem)">
          <span>{{doingItem.name}}</span>
          <button @click="del(doingItem.id)">删除</button>
        </li>
      </ul>
    </div>
    <div class="done">
      <h1>已经完成</h1>
      <ul class="donelist">
        <li v-for="doneItem in doneList"
            :key="doneItem.id">
          <input type="checkbox"
                 checked
                 @click="changeDone(doneItem)">
          <span>{{doneItem.name}}</span>
          <button @click="del(doneItem.id)">删除</button>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
import { setStorage, getStorage } from '../utils/storge'
export default {
  name: "about",
  data () {
    return {
      txt: "",//输入框值
      todoList: [
      ]
    }
  },
  created () {
    // 获取localStroage的数据
    this.todoList = getStorage("todolist") ? getStorage("todolist") : [];
  },
  methods: {
    send () {
      this.todoList.push({ name: this.txt, isdone: false, id: new Date().getTime() })
      // this.todoList = [...this.todoList, { name: this.txt, isdone: false, id: new Date().getTime() }]
      console.log(this.todoList)
      setStorage("todolist", this.todoList)
      this.txt = "";//清空输入框
    },
    changedoing (item) { //切换到已完成
      item.isdone = true;
      console.log(this.todoList)
      setStorage("todolist", this.todoList)

    },
    changeDone (item) { //切换到未完成
      item.isdone = false;
      setStorage("todolist", this.todoList)
    },
    del (id) {//删除
      this.todoList.forEach((ele, index) => {
        if (ele.id == id) {
          this.todoList.splice(index, 1)
        }
      })
      setStorage("todolist", this.todoList)
    }
  },
  computed: {
    dongingList () { //正在进行的数据
      return this.todoList.filter((ele, index) => {
        return ele.isdone == false
      })
    },
    doneList () { //已完成的数据
      return this.todoList.filter((ele, index) => {
        return ele.isdone == true
      })
    }
  }
}
</script>

<style scoped lang="less">
// scoped 让样式只在当前组件生效  互不影响
* {
  margin: 0;
  padding: 0;
}
.title {
  width: 100%;
  background: black;
  height: 70px;
  line-height: 70px;
  color: white;
  div {
    width: 80%;
    margin: 0 auto;
    span {
      font-size: 25px;
    }
    input {
      width: 300px;
      height: 40px;
      margin-left: 50px;
    }
  }
}
.doing,
.done {
  background: skyblue;
}
.list {
  width: 80%;
  margin: 0 auto;
  list-style: none;
  li {
    line-height: 40px;
    height: 40px;
    background: white;
    margin-top: 20px;
    display: flex;
    justify-content: space-around;
    align-items: center;
  }
}
.donelist {
  width: 80%;
  margin: 0 auto;
  li {
    line-height: 40px;
    height: 40px;
    background: #ccc;
    margin-top: 20px;
    display: flex;
    justify-content: space-around;
    align-items: center;
  }
}
</style>
